package netty.executor;

import io.netty.channel.DefaultEventLoopGroup;
import netty.util.StartAndStop;
import test.PerformanceAnalyze;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class EventLoopGroupScheduleZetas extends StartAndStop {

    PerformanceAnalyze performanceAnalyze = new PerformanceAnalyze();

    int size = 4000;

    ScheduledExecutorService scheduledExecutorService0 = Executors.newScheduledThreadPool(16);
    ScheduledExecutorService scheduledExecutorService1 = Executors.newScheduledThreadPool(16);

//    NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(64);
    DefaultEventLoopGroup nioEventLoopGroup = new DefaultEventLoopGroup(64);

    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(8);

    public void start() {

        for (int i = 0; i < size; i++) {
            int period = 100;
            nioEventLoopGroup.scheduleAtFixedRate(new DelayAnalyze(), period, period, TimeUnit.MICROSECONDS);
        }
    }

    class DelayAnalyze implements Runnable {

        long nextTime = System.nanoTime();

        @Override
        public void run() {
            performanceAnalyze.meter();
            performanceAnalyze.delay(System.nanoTime() - nextTime);

            this.nextTime = System.nanoTime();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new EventLoopGroupScheduleZetas().start();
        new CountDownLatch(1).await();
    }

}
